<?php
if(!defined('IN_CORE')) exit('permission deny');
/**
 * 登录类
 * @author zhupp
 * @version $Id: access.php,v 1.0 2012/04/23 14:39:32 $
 * @example 
 * require_once './access.php';
 * access::init();
 * access::login('name', 'password');
 */
class access {
	/**
	 * 表名
	 * @var string
	 */
	private static $table = '';
	
	/**
	 * 表字段映射,value为表里的真实字段名
	 */
	private static $fields = array(
		'uid'=>'id', //用户唯一标识id
		'username'=>'uname', //用户名
		'password'=>'pwd', //密码
		/**
		 * 以下字段如果没有可以注释
		 */
		'lastLoginTime' => 'logintime', //最后登录时间
		'lastLoginIP' => 'loginip', //最后登录的IP
			
	);
	
	/**
	 * session名称
	 * @var string
	 */
	private static $sessionName = 'loginsession';
	
	/**
	 * cookie名称
	 * @var string
	 */
	private static $cookieName = 'logincookie';
	
	/**
	 * cookie有效时间 默认为一个月2592000(单位:秒)
	 * @var int
	 */
	private static $cookieTime = 2592000;
	
	/**
	 * cookie的域
	 * @var string
	 */
	private static $cookieDomain = '';
	
	/**
	 * 密码的加密方式
	 * @var string
	 */
	private static $encryptMethod = 'md5';
	
	/**
	 * 载入
	 */
	public static function init($settings = array()) {
		//载入自定义配置
		if (is_array($settings) && !empty($settings)) {
			foreach($settings as $k => $v) {
				if (!isset(self::$$k)) {
					self::debug('属性 '.$k.' 不存在');
					exit;
				}				
				self::$$k = $v;
			}
		}
		
		//启动session
		if(!isset($_SESSION)) session_start();
		
		//设置默认值
		if(empty(self::$cookieDomain)) self::$cookieDomain = $_SERVER['HTTP_HOST'];
	}

	/**
	 * 登录
	 * @param string $table
	 * @param int $id
	 */
	public static function login($username, $password, $remember = true) {
		$sql = "SELECT ".self::$fields['uid']." AS `uid` FROM ".self::$table;
		$sql .=" WHERE ".self::$fields['username']." =? ";
		$sql .=" AND ".self::$fields['password']." = ? LIMIT 1;";
		$method = self::$encryptMethod;		
		eval("\$password = $method($password);");
		$res = db::query($sql, array($username, $password), 'one');

		if($res['uid']) {
			session::set(self::$fields['uid'], $res['id']);
			self::updateLoginInfo($table, $res['id']);
			if($remember) {
				$cookie = serialize(array('username'=>$res[self::$fields['username']],'ip'=>self::getIP(), 'lastLoginTime'=>time()));
				cookie::set(self::$cookieName, $cookie, true, time()+self::$cookieTime, '/', self::$cookieDomain, false, true);
			}
			return true;
		}else return false;
	}
	
	public static function register($name, $password) {
		
	}
	
	/**
	 * 是否已经登录
	 */
	public static function isLoginned() {
		$login_flag = session::get(self::$fields['uid']);
		if(is_null($login_flag)) showMsg('正在转向登陆页面', './login.php');
		else echo '您已经登录';
	}
	
	/**
	 * 退出
	 */
	public static function userExit($redirectTo = '') {
		session::delAll();
		cookie::del(self::$cookieName);

		if(!empty($redirectTo) && !headers_sent()) {
			header('Location: '.$redirectTo );			
		}
		
		exit;
	}
	
	/**
	 * 更新登录信息
	 */
	public static function updateLoginInfo($table, $id) {
		if(!self::$fields['lastLoginTime'] || !self::$fields['lastLoginIP']) return;
		
		$prepare = '';
		$prepare .= "UPDATE ".self::$table." SET ".self::$fields['lastLoginTime']." = ? ";
		$prepare .= ", ".self::$fields['lastLoginIP']." = ? WHERE `id` = ?";

		db::execute($prepare, array(time(), self::getIP(), $id));
	}
	
	/**
	 * 获取客户端IP
	 */
	public static function getIP() {
		return !empty($_SERVER['REMOTE_ADDR']) ? ip2long($_SERVER['REMOTE_ADDR']) : '';
	}
	
	/**
	 * 打印出错信息
	 * @param mixed $msg
	 */
	private static function debug($msg) {
		debug::output($msg);
	}
	
	
}